package com.weaver.drools.common.utils;

import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weaver.drools.common.utils.Json2Util;
/**
 * 
*  @application name： controller json数据处理
*  @author: zhouxinlei
*  @time：2018年7月18日
*  @version：ver 1.1
 */
public class ControllerUtil {
	
	
	public static void writejson(HttpServletResponse response,String json) {
		response.setContentType("application/json;charset=utf-8");
		if (json != null) {
			try {
				response.getWriter().write(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String messageData(Object data) {
		return Json2Util.toJsonString(data);
	}

	public static void writejsonAndSession(HttpServletResponse response, HttpServletRequest request, String json) {
		response.setContentType("application/json;charset=utf-8");
		if (json != null) {
			try {
				response.getWriter().write(json);
				request.getSession(true).setAttribute("data", json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
}
